package com.safe.ly_safe.base;

import android.app.Activity;
import android.content.Context;
import android.os.StrictMode;
import android.util.Log;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import com.tencent.smtt.sdk.QbSdk;
import java.util.ArrayList;
import java.util.List;

/**
 * @PACKAGE :com.safe.ly_safe.base
 * @Author : HSF
 * @Time : On 2022/4/22 15:17
 * @Description : BaseApplication
 */
public class BaseApplication extends MultiDexApplication {

    //volatile关键字能够保证可见性，被volatile修饰的变量，在一个线程中被改变时会立刻同步到主内存中，而另一个线程在操作这个变量时都会先从主内存更新这个变量的值
    private volatile static BaseApplication instance = null;

    private List<Activity> antipovertyStack = new ArrayList<>();

    private static final String BUGLY_API_KEY = "";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //初始化腾讯X5内核
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                //x5内核初始化完成回调接口，此接口回调并表示已经加载起来了x5，有可能特殊情况下x5内核加载失败，切换到系统内核。
                Log.i("BaseApplication","x5内核初始化完成");
            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.i("BaseApplication","x5内核加载是否成功:"+b);
            }
        });
    }

    public static BaseApplication getInstance() {
        return instance;
    }


    /**
     添加进入任务栈
     */
    public void joinTaskStack(Activity activity){
        if (!antipovertyStack.contains(activity)){
            antipovertyStack.add(activity);
        }
    }

    /**
     * 退出任务栈
     */
    public void quitTaskStack(Activity activity){
        if (antipovertyStack.contains(activity)){
            antipovertyStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 退出所有Activity,及关闭整个应用
     */
    public void exitAll(){
        for (Activity activity: antipovertyStack){
            activity.finish();
        }
    }

    protected void setStrictMode() {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
    }

}
